<template>
    <el-dialog
        title="机构设置"
        :visible.sync="dialogVisible"
        @open="open"
        @closed="close"
        top="15px"
        width="600px">
        <el-form :model="formdata" label-width="120px" ref="addform"  :rules="rules">
            <el-form-item label="机构名称：" prop="bRealName">
                <el-input type="text" v-model.trim="formdata.bRealName" placeholder="请输入机构名称"></el-input>
            </el-form-item>
            <el-form-item label="学段：" prop="bMSN">
            <el-select v-model="formdata.bMSN" placeholder="请选择学段">
                <el-option :label="item.Code_Name" :value="item.Code_No" v-for="item in SECTION" :key="item.Code_No"></el-option>
            </el-select>
        </el-form-item>
            <el-form-item label="机构地址：" prop="bPicture">
                <el-cascader v-model="formdata.bPicture" :options="cityList" clearable placeholder="请选择"></el-cascader>
            </el-form-item>
            <el-form-item label="" prop="bSignature">
                <el-input type="text" v-model.trim="formdata.bSignature" placeholder="详细地址"></el-input>
            </el-form-item>
            <el-form-item label="联系人：" prop="bAlias">
                <el-input type="text" v-model.trim="formdata.bAlias" placeholder="联系人"></el-input>
            </el-form-item>
            <el-form-item label="机构电话：" prop="bMobile">
                <el-input type="text" v-model.trim="formdata.bMobile" placeholder="电话"></el-input>
            </el-form-item>
            <el-form-item label="在校人数：" prop="bIsOA_Search">
                <el-input type="text" v-model.trim="formdata.bIsOA_Search" placeholder="在校人数"></el-input>
            </el-form-item>
            <el-form-item label="备注：" prop="bMem1">
                <el-input type="textarea" v-model="formdata.bMem1" :autosize="{ minRows: 4}"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="save">确 定</el-button>
        </div>
    </el-dialog>
</template>
<script>
import {editSysUser,getSysUser} from '@/api/index.js'
import {validateContent} from '@/common/js/validate.js'
export default {
    data() {
        return {
            dialogVisible:false,
            cityList:[],
            formdata:{
                bRealName:'',
                bPicture:[],
                bSignature:'',
                bAlias:'',
                bMobile:'',
                bIsOA_Search:'',
                bMem1:'',
                bMSN:''
            },
            rules: {
                bRealName: [
                    { required: true, message: '机构名称不能为空', trigger: 'blur' }
                ],
                bMSN: [
                    { required: true, message: '请选择学段', trigger: 'change' }
                ],
                bMobile: [
                    { validator: validateContent, trigger: 'blur' }
                ],
            }
        }
    },
    props:{
        SECTION:{
            type:Array,
            default:function() {
                return []
            }
        }
    },
    created() {
        this.$util.city_lop(this.cityList);
    },
    methods:{
        open(){
            getSysUser().then((res) => {
                if(res && res.length) {
                    this.formdata = {
                        bRealName : res[0].RealName ||'',
                        bPicture : res[0].Picture ? res[0].Picture.split('-') : [],
                        bSignature : res[0].Signature || '',
                        bAlias : res[0].Alias || '',
                        bMobile : res[0].Mobile || '',
                        bIsOA_Search : res[0].IsOA_Search || '',
                        bMem1 : res[0].Mem1 || '',
                        bMSN : res[0].MSN || ''
                    }
                }
            })
        },
        save(){
            this.$refs.addform.validate((valid) => {
                if (valid) {
                    let params = {
                        ...this.formdata,
                        bPicture:this.formdata.bPicture.join('-')
                    };
                    editSysUser(params).then((res) => {
                        if(res) {
                            if(res.code == 200) {
                                this.$util.tipsMsg('success','修改成功！');
                                this.dialogVisible = false;
                            } else {
                                this.$util.tipsMsg('error',res.message);
                            }
                        }
                    }).catch((err) => {

                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            })
            
        },
        close(){
            this.$refs.addform.resetFields();
        }
    }
}
</script>
<style lang="less" scoped>

</style>
